<%-- 
    Document   : vehicle
    Created on : 2013-5-7, 11:42:30
    Author     : piaohao
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
    <head>
        <base href="<%= basePath%>"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <%@include file="../main/common.jsp" %>
        <title>车辆档案</title>
        <script type="text/javascript">
            Ext.onReady(function() {
                var vehicleForm = Ext.create("Ext.form.Panel", {
                    url: "vehicle/add",
                    bodyPadding: 10,
                    layout: {
                        type: "table",
                        columns: 2
                    },
                    defaults: {
                        labelAlign: "right"
                    },
                    defaultType: "textfield",
                    items: [
                        {fieldLabel: "车牌号", name: "vehicle.plate_number"},
                        {fieldLabel: "注册登记日期", name: "veh_reg_date", xtype: "datefield", editable: false, format: "Y-m-d"},
                        {
                            xtype: "combo",
                            fieldLabel: '类型等级',
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "普通级轿车", value: "普通级轿车"},
                                    {text: "中级轿车", value: "中级轿车"},
                                    {text: "高一级轿车", value: "高一级轿车"},
                                    {text: "高二级轿车", value: "高二级轿车"},
                                    {text: "小型普通级", value: "小型普通级"},
                                    {text: "小型中级", value: "小型中级"},
                                    {text: "小型高一级", value: "小型高一级"},
                                    {text: "小型高二级", value: "小型高二级"},
                                    {text: "中型普通级", value: "中型普通级"},
                                    {text: "中型中级", value: "中型中级"},
                                    {text: "中型高一级", value: "中型高一级"},
                                    {text: "中型高二级", value: "中型高二级"},
                                    {text: "大型普通级", value: "大型普通级"},
                                    {text: "大型中级", value: "大型中级"},
                                    {text: "大型高一级", value: "大型高一级"},
                                    {text: "大型高二级", value: "大型高二级"},
                                    {text: "大型高三级", value: "大型高三级"},
                                    {text: "大型卧铺中级", value: "大型卧铺中级"},
                                    {text: "大型卧铺高一级", value: "大型卧铺高一级"},
                                    {text: "大型卧铺高二级", value: "大型卧铺高二级"}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "vehicle.type",
                            emptyText: "--请选择--",
                            value: ""
                        },
                        {fieldLabel: "核定载客（人）", name: "vehicle.person_count"},
                        {fieldLabel: "自编号", name: "vehicle.private_number"},
                        {fieldLabel: "道路运输证号", name: "vehicle.road_trans_num"},
                        {
                            xtype: "combo",
                            fieldLabel: '车牌颜色',
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "黄色", value: "黄色"},
                                    {text: "蓝色", value: "蓝色"}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "vehicle.plate_color",
                            value: "黄色"
                        },
                        {fieldLabel: "车身颜色", name: "vehicle.veh_color"},
                        {fieldLabel: "厂牌", name: "vehicle.factory"},
                        {fieldLabel: "型号", name: "vehicle.model"},
                        {fieldLabel: "备注", name: "vehicle.remark"},
                        {
                            id: "orgCombo",
                            xtype: "combo",
                            labelAlign: "right",
                            fieldLabel: "所属机构",
                            name: "vehicle.org_id",
                            displayField: 'name',
                            valueField: 'id',
                            editable: false,
                            emptyText: "--请选择--",
                            store: {
                                fields: ['name', 'id'],
                                proxy: {
                                    type: 'ajax',
                                    url: 'org/getAll',
                                    reader: {
                                        type: 'json'
                                    }
                                }
                            }
                        },
                        {name: "vehicle.id", xtype: "hiddenfield"}
                    ],
                    buttons: [{
                            text: '重置',
                            iconCls: "refresh",
                            handler: function() {
                                this.up('form').getForm().reset();
                            }
                        }, {
                            text: '提交',
                            formBind: true, //only enabled once the form is valid
                            disabled: true,
                            iconCls: "save",
                            handler: function() {
                                var form = this.up('form').getForm();
                                if (form.isValid()) {
                                    form.submit({
                                        success: function(form, action) {
                                            var json = action.result;
                                            Ext.Msg.alert('提示', json.msg, function() {
                                                vehicleWindow.close();
                                            });
                                        },
                                        failure: function(form, action) {
                                            Ext.Msg.alert('提示', action.result.msg);
                                        }
                                    });
                                }
                            }
                        }, {
                            text: "关闭",
                            iconCls: "close",
                            handler: function() {
                                vehicleWindow.close();
                            }
                        }]
                });
                var vehicleWindow = Ext.create("Ext.window.Window", {
                    title: "车辆档案",
                    width: 550,
                    height: 300,
                    maximizable: true,
                    closeAction: "hide",
                    layout: "fit",
                    modal: true,
                    items: [vehicleForm]
                });
                var vehicleStore = Ext.create('Ext.data.Store', {
                    fields: ["id", "plate_number", "veh_reg_date", "type", "person_count", "private_number", "road_trans_num", "plate_color", "veh_color", "factory", "model", "remark", "org_id"],
                    pageSize: 15,
                    proxy: {
                        type: 'ajax',
                        url: 'vehicle/list',
                        reader: {
                            type: 'json',
                            root: 'data'
                        }
                    },
                    autoLoad: true
                });
                var vehicleGrid = Ext.create("Ext.grid.Panel", {
                    title: "车辆档案",
                    columnLines: true,
                    frame: true,
                    selType: 'checkboxmodel', // 设置选择模式 
                    multiSelect: true, // 允许多选
                    forceFit: true,
                    columns: [
                        {header: '主键', dataIndex: 'id', hidden: true},
                        {header: '车牌号', dataIndex: 'plate_number', width: 120},
                        {header: '注册登记日期', dataIndex: 'veh_reg_date', width: 200},
                        {header: '类型等级', dataIndex: 'type', width: 150},
                        {header: '核定载客（人）', dataIndex: 'person_count', width: 100},
                        {header: '自编号', dataIndex: 'private_number', width: 100},
                        {header: '道路运输证号', dataIndex: 'road_trans_num', width: 120},
                        {header: '车牌颜色', dataIndex: 'plate_color', width: 100},
                        {header: '车身颜色', dataIndex: 'veh_color', width: 100},
                        {header: '厂牌', dataIndex: 'factory', width: 100},
                        {header: '类型', dataIndex: 'model', width: 100},
                        {header: '备注', dataIndex: 'remark', width: 100},
                        {header: '所属机构', dataIndex: 'org_id', hidden: true}
                    ],
                    store: vehicleStore,
                    tbar: [
                        {xtype: "button", text: "查看", iconCls: "search",
                            handler: function() {
                                var sel = vehicleGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0].data;
                                var detailWindow = Ext.create("Ext.window.Window", {
                                    title: "车辆档案",
                                    width: 800,
                                    height: 400,
                                    maximizable: true,
                                    modal: true,
                                    closeAction: "hide",
                                    layout: "fit",
                                    html: "<iframe id='detailFrame' src='vehicle/showDetail/" + record.id + "' width='100%' height='100%'></iframe>",
                                    buttons: [
                                        {text: "打印", iconCls: "print",
                                            handler: function() {
                                                Ext.get("detailFrame").dom.contentWindow.print();
                                            }},
                                        {text: "关闭", iconCls: "close",
                                            handler: function() {
                                                detailWindow.close();
                                            }}
                                    ]
                                });
                                detailWindow.show();
                            }},
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "登记", iconCls: "add",
                            handler: function() {
                                vehicleForm.getForm().reset();
                                vehicleForm.getForm().url = "vehicle/add";
                                vehicleWindow.show();
                            }},
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "变更", iconCls: "edit",
                            handler: function() {
                                var sel = vehicleGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0];
                                vehicleForm.getForm().url = "vehicle/update";
                                vehicleWindow.show();
                                vehicleForm.getForm().setValues({
                                    "vehicle.id": record.get("id"),
                                    "vehicle.plate_number": record.get("plate_number"),
                                    "veh_reg_date": record.get("veh_reg_date").substring(0, 10),
                                    "vehicle.type": record.get("type"),
                                    "vehicle.person_count": record.get("person_count"),
                                    "vehicle.private_number": record.get("private_number"),
                                    "vehicle.road_trans_num": record.get("road_trans_num"),
                                    "vehicle.plate_color": record.get("plate_color"),
                                    "vehicle.veh_color": record.get("veh_color"),
                                    "vehicle.factory": record.get("factory"),
                                    "vehicle.model": record.get("model"),
                                    "vehicle.remark": record.get("remark"),
                                    "vehicle.org_id": record.get("org_id")
                                });
                                Ext.getCmp("orgCombo").getStore().load();
                            }
                        },
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "删除", iconCls: "remove",
                            handler: function() {
                                Ext.Msg.confirm("提示", "删除不可恢复，您确定删除吗？", function(id) {
                                    if (id === "yes") {
                                        var mask = new Ext.LoadMask(vehicleGrid, {msg: "正在删除..."});
                                        mask.show();
                                        var sel = vehicleGrid.getSelectionModel().getSelection();
                                        var ids = [];
                                        for (i = 0; i < sel.length; i++) {
                                            ids.push(sel[i].data.id);
                                        }
                                        Ext.Ajax.request({
                                            url: "vehicle/delete",
                                            params: {
                                                ids: ids
                                            },
                                            success: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    vehicleGrid.getStore().remove(sel);
                                                    mask.hide();
                                                });
                                            },
                                            failure: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    mask.hide();
                                                });
                                            }
                                        });
                                    }
                                });
                            }
                        }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: vehicleStore,
                        displayInfo: true
                    })
                });
                var viewport = Ext.create("Ext.Viewport", {
                    layout: {type: "border"},
                    items: [
                        {region: "center", layout: "fit", items: [vehicleGrid]}
                    ]
                });
            });
        </script>
    </head>
    <body>
    </body>
</html>
